// view/my/diancan/addcai/addcai.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    buttonClicked: false,
    yuming: yuming,
    heights: '',
    titles: "",
    isChecked1: false, //多规格
    ggs: [{}, {}, {}, {}],
    isgui: false,
    iddazhe: false, //是否打折
    isshang: false, //是否上架
    ssfllist: [], //所属分类
    ssflindex: 0,
    ssflguo: '请选择所属分类', //所属分类
    fid: 0, //所属id
    title: '', //菜品名称
    cai: '', //菜品图片
    focusInput: false,
    height: '',
    isInput: false,
    fbnr: '', //属性名称
    sx: [], //属性名
    leixmc: '', //类型名称
    lsisShow: false,
    lxshous: 0,
    page: 1,
    guiguid: '', //规格id
    oriPrice: '', //原价
    shoumj: '', //售卖价
    mrkc: '', //每日库存
    isDiscount: '', //0否1是
    paixu: '', //排序
    isPut: '', //0上架 -1下架
    goodsAttr: [],
    spdw: '', //商品单位
    discountRate: '', //折扣率
    discountPrice: '', //折扣后价格
    Quota: '', //限购
    subClassindex: 0,
    iser: false,
    cpms: '', //菜品描述
    state: '', //添加 1 修改
    id: '', //修改id
    cName: '',
    isxgxq: '', //修改详情展示否
    isxgxqaa: false,
    ixsindex: '',
    isxgxqid: '', //要修改规格的id
  },
  bindisxgxqaa() {
    this.setData({
      isxgxqaa: !this.data.isxgxqaa
    })
    if (this.data.isxgxqaa) {
      this.setData({
        ixsindex: 0
      })
    } else {
      this.setData({
        ixsindex: 1
      })
    }
  },
  // 编辑
  bianji(e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      isxgxq: !this.data.isxgxq,
      isxgxqid: id
    })
    this.xgxxq(id)
  },
  // 规格修改详情
  xgxxq(id) {
    this.myRequest(yuming + 'hallfood/goodsFindAttribute', {
      shopId: wx.getStorageSync('shopId'),
      id: id,
    }, "POST").then(res => {
      this.setData({
        leixmc: res.data.data.result.comType,
        xxgqlist: res.data.data.result.attValue,
      })
      if (res.data.data.result.isShow == 0) {
        this.setData({
          isxgxqaa: true,
          ixsindex: 0
        })
      } else {
        this.setData({
          isxgxqaa: false,
          ixsindex: 1
        })
      }
    })
  },
  isxgxq() {
    this.setData({
      isxgxq: !this.data.isxgxq
    })
  },
  // 规格修改保存
  xgxqsave() {
    var that = this
    that.setData({
      buttonClicked: true
    })
    var arrs = JSON.stringify(this.data.xxgqlist)
    this.myRequest(yuming + 'hallfood/goodsSaveAttribute', {
      shopId: wx.getStorageSync('shopId'),
      id: this.data.isxgxqid,
      comType: this.data.leixmc,
      isShow: this.data.ixsindex,
      attValue: arrs,
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        this.setData({
          isxgxq: false
        })
        setTimeout(function () {
          that.setData({
            buttonClicked: false
          })
        }, 1000)
        this.duogui()
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  // 多规格列表
  xiuxq() {
    var that = this
    this.myRequest(yuming + 'hallfood/goodsFindGoods', {
      shopId: wx.getStorageSync('shopId'),
      id: that.data.id
    }, 'POST').then(res => {
      var data = res.data.data
      if (data.goodsAttr.length == 0) {
        that.setData({
          isChecked1: false
        })
      } else {
        for (var i = 0; i < data.goodsAttr.length; i++) {
          data.goodsAttr[i].isCheck = true
        }
        that.setData({
          isChecked1: true,
        })
        var goodsAttr = data.goodsAttr
        var ggs = that.data.ggs
        for (var i = 0; i < ggs.length; i++) {
          for (var g = 0; g < ggs[i].attValue.length; g++) {
            for (var k = 0; k < goodsAttr.length; k++) {
              if (ggs[i].attValue[g].id == goodsAttr[k].attValueId) {
                ggs[i].attValue[g].isCheck = true
              }
            }
          }
        }
        that.setData({
          ggs: ggs
        })
      }
      // 是否0否1是  打折
      if (data.isDiscount == 0) {
        that.setData({
          iddazhe: false
        })
      } else {
        that.setData({
          iddazhe: true
        })
      }
      // 是否上架 0上 -1 下
      if (data.isPut == 0) {
        that.setData({
          isshang: true
        })
      } else {
        that.setData({
          isshang: false
        })
      }
      that.setData({
        cai: data.goodsLogo, //菜品照片
        wucai: data.goodsLogo_thum, //无域名的菜图片
        title: data.title, //菜品名称
        cpms: data.content, //菜品描述
        oriPrice: data.oriPrice, //原价
        shoumj: data.prePrice, //售卖价
        mrkc: data.daystock, //每日库存
        isDiscount: data.isDiscount, //是否打折
        discountRate: data.discountRate, //折扣率
        discountPrice: data.discountPrice, //折扣后价格
        Quota: data.Quota, //限购
        spdw: data.unit, //菜品单位
        isPut: data.isPut, //是否上架
        paixu: data.solt, //排序
        fid: data.cid, // cid二级id
        ssflguo: data.fName, //一级名称
        // cid: data.cid, //二级id
        cName: data.cName, //二级名称
      })
      if (that.data.fid != 0) {
        that.setData({
          iser: true
        })
      }
    })
  },
  bindsubClass(e) {
    this.setData({
      subClassindex: e.detail.value,
      fid: this.data.subClass[e.detail.value].id,
      cName: this.data.subClass[e.detail.value].className
    })
  },
  // 规格多选
  guixuan(e) {
    var index = e.currentTarget.dataset.index //父类
    var indexs = e.currentTarget.dataset.indexs //子类
    var item = this.data.ggs
    var it = this.data.goodsAttr
    item[index].attValue[indexs].isCheck = !item[index].attValue[indexs].isCheck
    var list = []
    for (var i = 0; i < item.length; i++) {
      for (var k = 0; k < item[i].attValue.length; k++) {
        if (item[i].attValue[k].isCheck) {
          var arr = {
            attValueId: item[i].attValue[k].id,
            attTypeId: item[i].id,
            valueName: item[i].attValue[k].attName
          }
          list.push(arr)
        }
      }
    }
    this.setData({
      goodsAttr: list,
      ggs: item
    })
  },
  // 多规格列表
  duogui() {
    var that = this
    this.myRequest(yuming + 'hallfood/goodsSelectAttribute', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page
    }, 'POST').then(res => {
      that.setData({
        ggs: res.data.data.result
      })
    })
  },
  fbnr(e) {
    this.setData({
      fbnr: e.detail.value
    })
  },
  inputFocus(e) {
    var aaa = e.currentTarget.dataset.aaa
    if (aaa == 1) {
      this.setData({
        xgaaa: 1
      })
    }
    this.setData({
      height: e.detail.height,
      isInput: true,
      focusInput: true
    })
  },
  inputBlur(e) {
    this.setData({
      isInput: false
    })
  },
  // 确定
  fasong() {
    this.setData({
      isInput: false,
    })
    if (this.data.xgaaa == 1) {
      var dd = {
        attName: this.data.fbnr
      }
      if (this.data.fbnr == '') {
        wx.showToast({
          title: '请输入',
          icon: 'none'
        })
      } else {
        this.setData({
          xxgqlist: this.data.xxgqlist.concat(dd),
          fbnr: ''
        })
      }
    } else {
      var ss = {
        attName: this.data.fbnr
      }
      if (this.data.fbnr == '') {
        wx.showToast({
          title: '请输入',
          icon: 'none'
        })
      } else {
        this.setData({
          sx: this.data.sx.concat(ss),
          fbnr: ''
        })
      }
    }
  },
  yichu(e) {
    var index = e.currentTarget.dataset.index
    this.data.sx.splice(index, 1)
    this.setData({
      sx: this.data.sx
    })
  },
  xqyichu(e) {
    var index = e.currentTarget.dataset.index
    this.data.xxgqlist.splice(index, 1)
    this.setData({
      xxgqlist: this.data.xxgqlist
    })
  },
  // 评论
  focusButn: function () {
    this.setData({
      focusInput: true
    })
  },
  // 所属分类
  bindssfl(e) {
    if (this.data.ssfllist[e.detail.value].subClass.length == 0) {
      this.setData({
        iser: false,
        ssflguo: this.data.ssfllist[e.detail.value].className,
        fid: this.data.ssfllist[e.detail.value].id
      })
    } else {
      this.setData({
        iser: true,
        ssflguo: this.data.ssfllist[e.detail.value].className,
        subClass: this.data.ssfllist[e.detail.value].subClass,
        cName: this.data.ssfllist[e.detail.value].subClass[0].className
      })
      var ids = this.data.subClass[0].id
      this.setData({
        fid: ids
      })
    }
  },
  // 菜品名称
  title(e) {
    this.setData({
      title: e.detail.value
    })
  },
  // 分类列表
  canzlist(e) {
    var that = this
    this.myRequest(yuming + 'hallfood/goodsSelectClass', {
      shopId: wx.getStorageSync('shopId'),
      fid: that.data.fid
    }, 'POST').then(res => {
      that.setData({
        ssfllist: that.data.ssfllist.concat(res.data.data.result)
      })
    })
  },
  // 原价
  oriPrice(e) {
    this.setData({
      oriPrice: e.detail.value
    })
    if (this.data.state == 1) {
      this.setData({
        discountPrice: (this.data.oriPrice) * (this.data.discountRate / 10).toFixed(2)
      })
    }
  },
  // 售卖价
  shoumj(e) {
    this.setData({
      shoumj: e.detail.value
    })
  },
  // 每次库存
  mrkc(e) {
    this.setData({
      mrkc: e.detail.value
    })
  },
  // 保存验证
  baocun() {
    if (this.data.cai == '') {
      wx.showToast({
        title: '请上传菜品照片',
        icon: 'none'
      })
    } else if (this.data.title == '') {
      wx.showToast({
        title: '请输入菜品名称',
        icon: 'none'
      })
    } else if (this.data.ssflguo == '请选择所属分类') {
      wx.showToast({
        title: '请选择所属分类',
        icon: 'none'
      })
    } else if (this.data.oriPrice == '') {
      wx.showToast({
        title: '请输入原价',
        icon: 'none'
      })
    } else if (this.data.shoumj == '') {
      wx.showToast({
        title: '请输入售卖价',
        icon: 'none'
      })
    } else if (this.data.mrkc == '') {
      wx.showToast({
        title: '请输入每日库存',
        icon: 'none'
      })
    } else if (this.data.spdw == '') {
      wx.showToast({
        title: '请输入商品单位',
        icon: 'none'
      })
    } else {
      this.addcai()
    }
  },
  // 菜品描述
  cpms(e) {
    this.setData({
      cpms: e.detail.value
    })
  },
  // 添加菜品接口
  addcai(e) {
    var goodsAttr = JSON.stringify(this.data.goodsAttr)
    var that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'hallfood/goodsAddGoods', {
      shopId: wx.getStorageSync('shopId'),
      goodsLogo: that.data.wucai, //菜的图片
      title: that.data.title, //菜名
      cid: that.data.fid, //所属分类
      goodsAttr: goodsAttr, //多规格
      oriPrice: that.data.oriPrice, //原价
      prePrice: that.data.shoumj, //售卖价
      daystock: that.data.mrkc, //每日库存
      isDiscount: that.data.isDiscount, //是否打折
      discountRate: that.data.discountRate, //折扣率
      discountPrice: that.data.discountPrice, //折扣后价格
      Quota: that.data.Quota, //限购
      unit: that.data.spdw, //商品单位
      isPut: that.data.isPut, //是否上下架
      solt: that.data.paixu, //排序
      content: that.data.cpms, //菜品描述
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0,
          })
          that.setData({
            buttonClicked: false
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  // 修改菜品接口
  xiugai(e) {
    var goodsAttr = JSON.stringify(this.data.goodsAttr)
    var that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'hallfood/goodsSaveGoods', {
      id: that.data.id,
      shopId: wx.getStorageSync('shopId'),
      goodsLogo: that.data.wucai, //菜的图片
      title: that.data.title, //菜名
      cid: that.data.fid, //所属分类
      goodsAttr: goodsAttr, //多规格
      oriPrice: that.data.oriPrice, //原价
      prePrice: that.data.shoumj, //售卖价
      daystock: that.data.mrkc, //每日库存
      isDiscount: that.data.isDiscount, //是否打折
      discountRate: that.data.discountRate, //折扣率
      discountPrice: that.data.discountPrice, //折扣后价格
      Quota: that.data.Quota, //限购
      unit: that.data.spdw, //商品单位
      isPut: that.data.isPut, //是否上下架
      solt: that.data.paixu, //排序
      content: that.data.cpms, //菜品描述
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0,
          })
          that.setData({
            buttonClicked: false
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  // 折扣率
  discountRate(e) {
    this.setData({
      discountRate: e.detail.value,
    })
    if (this.data.oriPrice == '') {
      wx.showToast({
        title: '请输入原价',
        icon: 'none'
      })
      e.detail.value = ''
      this.setData({
        discountRate: 0
      })
    } else {
      this.setData({
        discountPrice: (this.data.oriPrice) * (this.data.discountRate / 10).toFixed(2)
      })
    }
  },
  // 限购
  Quota(e) {
    this.setData({
      Quota: e.detail.value
    })
  },
  // 放大
  fangda(e) {
    var that = this
    wx.showModal({
      title: '您是想实现哪种操作',
      cancelText: '重新上传',
      confirmText: '放大',
      success: function (res) {
        if (res.confirm) {
          console.log("放大")
          var img = e.currentTarget.dataset.img
          wx.previewImage({
            urls: [img]
          })
        } else {
          console.log("重新上传")
          var aaa = e.currentTarget.dataset.aaa
          //选取图片
          //选取图片
          wx.chooseImage({
            success(res) {
              const tempFilePaths = res.tempFilePaths
              wx.uploadFile({
                url: yuming + 'Besall/Upload',
                filePath: tempFilePaths[0],
                name: 'file',
                formData: {

                },
                success(res) {
                  var data = JSON.parse(res.data)
                  wx.showToast({
                    title: data.msg,
                    icon: 'none'
                  })
                  var urls = data.httpUrl + data.imgUrl
                  that.setData({
                    cai: urls,
                    wucai: data.imgUrl
                  })
                }
              })
            }
          })
        }
      }
    })
  },
  // 叉掉
  chadiao(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    wx.showModal({
      title: '您确定要删除该规格吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.myRequest(yuming + 'hallfood/goodsDelAttribute', {
            shopId: wx.getStorageSync('shopId'),
            id: id,
          }, "POST").then(res => {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
            if (res.data.code == 200) {
              that.duogui()
            }
          })
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 上传
  chuan(e) {
    var that = this;
    //选取图片
    wx.chooseImage({
      count: 1,
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: yuming + 'Besall/Upload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {

          },
          success(res) {
            var data = JSON.parse(res.data)
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
            var urls = data.httpUrl + data.imgUrl
            that.setData({
              cai: urls,
              wucai: data.imgUrl
            })
          }
        })
      }
    })
  },
  // 是否上架
  bindshang() {
    this.setData({
      isshang: !this.data.isshang
    })
    if (this.data.isshang) {
      this.setData({
        isPut: 0
      })
    } else {
      this.setData({
        isPut: -1
      })
    }
  },
  // 排序
  paixu(e) {
    this.setData({
      paixu: e.detail.value
    })
  },
  // 商品单位
  spdw(e) {
    this.setData({
      spdw: e.detail.value
    })
  },
  // 类型名称
  leixmc(e) {
    this.setData({
      leixmc: e.detail.value
    })
  },
  bindlsisShow() {
    this.setData({
      lsisShow: !this.data.lsisShow
    })
    if (this.data.lsisShow) {
      this.setData({
        lxshous: 0 //0开启 1关闭
      })
    } else {
      this.setData({
        lxshous: 1
      })
    }
  },
  // 确定
  que() {
    var that = this
    that.setData({
      isgui: !that.data.isgui
    })
    var arrs = JSON.stringify(that.data.sx)
    if (that.data.leixmc == '') {
      wx.showToast({
        title: '请输入类型名称',
        icon: 'none'
      })
    } else if (that.data.sx.length == 0) {
      wx.showToast({
        title: '请添加属性值',
        icon: 'none'
      })
    } else {
      this.myRequest(yuming + 'hallfood/goodsAddAttribute', {
        shopId: wx.getStorageSync('shopId'),
        comType: that.data.leixmc,
        isShow: that.data.lxshous,
        attValue: arrs
      }, 'POST').then(res => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 200) {
          that.setData({
            isgui: false,
            sx: [],
            lsisShow: false
          })
          that.duogui()
        }
      })
    }
  },
  // 是否打折
  bindazhe() {
    this.setData({
      iddazhe: !this.data.iddazhe
    })
    if (this.data.iddazhe) {
      this.setData({
        isDiscount: 1
      })
    } else {
      this.setData({
        isDiscount: 0
      })
    }
  },
  // 添加规格
  addguige() {
    this.setData({
      isgui: !this.data.isgui
    })
  },
  // 多规格
  changeSwitch1(e) {
    this.setData({
      isChecked1: e.detail.value
    })
  },
  backs() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      titles: options.title,
      state: options.state,
      id: options.id
      // ssflguo: this.data.ssfllist[this.data.ssflindex].className
    })
    this.canzlist()
    this.duogui()
    if (this.data.state == 1) {
      this.xiuxq()
    } else {
      // 多规格里边 类型是否开启
      if (this.data.lsisShow) {
        this.setData({
          lxshous: 0
        })
      } else {
        this.setData({
          lxshous: 1
        })
      }
      // 是否0否1是
      if (this.data.iddazhe) {
        this.setData({
          isDiscount: 1
        })
      } else {
        this.setData({
          isDiscount: 0
        })
      }
      // 是否上架 0上 -1 下
      if (this.data.isshang) {
        this.setData({
          isPut: 0
        })
      } else {
        this.setData({
          isPut: -1
        })
      }
      // 修改规格是否开启
      if (this.data.isxgxqaa) {
        this.setData({
          ixsindex: 0
        })
      } else {
        this.setData({
          ixsindex: 1
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})